package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.ReserveInfoBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.dialog.ImageViewpagerDialog;
import com.example.hotel_lxy.ui.dialog.SelectedInfoFoodDialog;
import com.example.hotel_lxy.ui.dialog.SelectedServiceDialog;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 预留信息
 */
@Route(path = "/main/ReservedInformationActivity")
public class ReservedInformationActivity extends BaseActivity {

    @Autowired
    String id;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.begin_time)
    TextView beginTime;
    @BindView(R.id.b_name)
    TextView bName;
    @BindView(R.id.desk)
    TextView desk;
    @BindView(R.id.book)
    TextView book;
    @BindView(R.id.mobile)
    TextView mobile;

    @BindView(R.id.increase_money)
    TextView increaseMoney;
    @BindView(R.id.director)
    TextView director;
    @BindView(R.id.source)
    TextView source;
    @BindView(R.id.addtime)
    TextView addtime;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.rv_contract)
    RecyclerView rvContract;
    @BindView(R.id.rv_other)
    RecyclerView rvOther;
    @BindView(R.id.detele)
    TextView detele;
    @BindView(R.id.text_yd)
    TextView textYd;
    @BindView(R.id.line_increase)
    LinearLayout lineIncrease;
    @BindView(R.id.text_d_name)
    TextView textDName;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    private List<String> mlist_hetong = new ArrayList<>();
    private List<String> mlist_qita = new ArrayList<>();
    private List<ContractsBean> mlist_rvhetong = new ArrayList<>();
    private List<OthersBean> mlist_rvqita = new ArrayList<>();

    @Autowired
    String is_del;
    public static void start(String id,String is_del) {
        ARouter.getInstance().build(ARouterPath.getReservedInformationActivity())
                .withString("id", id)
                .withString("is_del", is_del)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reserved_information;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void doWork() {
        super.doWork();
        ARouter.getInstance().inject(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        detele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDelete();
            }
        });
        textYd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetUpReserveActivity.start(id);
                finish();
            }
        });
        lineIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectedServiceDialog.show(ReservedInformationActivity.this, null, id,"","");
            }
        });
        textDName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectedInfoFoodDialog.show(ReservedInformationActivity.this, null, id,"","");
            }
        });
        showLatentDel();
    }

    private void showDelete() {
        OkHttpUtils.get()
                .url(Api.POST_LATENT_DELETE)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("id", "" + id)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "潜客预留信息Exception   " + e.getMessage());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "潜客预留信息onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            ToastUtils.show(ReservedInformationActivity.this, "" + beanBean.getMsg());
                            finish();
                        }else if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }

                    }
                });
    }

    private void showLatentDel() {
        OkHttpUtils.get()
                .url(Api.POST_SCHEDULED_ORDER_dEL)
                .addHeader("flag", SPUtils.get(ReservedInformationActivity.this, "token", "").toString())
                .addParams("id", "" + id)
                .addParams("is_del", "" + is_del)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "潜客预留信息Exception   " + e.getMessage());

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "潜客预留信息onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        ReserveInfoBean reserveInfoBean = JsonUtils.parseByGson(response, ReserveInfoBean.class);
                        if (beanBean.getCode().equals("0")) {
                            ReserveInfoBean.DataBeanX.DataBean data = reserveInfoBean.getData().getData();
                            beginTime.setText("【宴会时间】" + data.getBegin_time());
                            bName.setText("【宴会厅】" + data.getB_name() + "         " + data.getT_name());
                            desk.setText("【桌数】" + data.getDesk() + "桌 备" + data.getPrepare_desk() + "桌");

                            textDName.setText("" + data.getD_money());
                            book.setText("" + data.getBook());
                            mobile.setText("" + data.getMobile());
                            if (data.getIncrease_money() != null) {
                                increaseMoney.setText("" + data.getIncrease_money() + "元");
                                lineIncrease.setVisibility(View.VISIBLE);
                            } else {
                                lineIncrease.setVisibility(View.GONE);
                            }
                            director.setText("【客户经理】" + data.getDirector());
                            source.setText("【客户来源】" + data.getSource());
                            addtime.setText("【预订时间】" + data.getAddtime());
                            content.setText("" + data.getContent());
                            rvType.setLayoutManager(new LinearLayoutManager(ReservedInformationActivity.this));
                            RBaseAdapter<ReserveInfoBean.DataBeanX.NewArrBean> valueBeanRBaseAdapter = new RBaseAdapter<ReserveInfoBean.DataBeanX.NewArrBean>
                                    (ReservedInformationActivity.this,
                                            R.layout.item_reserve_info_rv_type, reserveInfoBean.getData().getNewArr()) {
                                @Override
                                protected void fillItem(RViewHolder holder, ReserveInfoBean.DataBeanX.NewArrBean item, int position) {
                                    TextView name = holder.itemView.findViewById(R.id.name);
                                    TextView names = holder.itemView.findViewById(R.id.names);
                                    TextView groomname = holder.itemView.findViewById(R.id.groomname);
                                    TextView groomphoto = holder.itemView.findViewById(R.id.groomphoto);
                                    name.setText("" + item.getTitle());
                                    names.setText("" + item.getMobile());
                                    if (item.getNames() != null) {
                                        groomname.setText("" + item.getNames());
                                    }
                                    if (item.getMobiles() != null) {
                                        groomphoto.setText("" + item.getMobiles());
                                    }
                                    groomphoto.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent intent = new Intent(Intent.ACTION_DIAL);
                                            Uri data = Uri.parse("tel:" + item.getMobiles());
                                            intent.setData(data);
                                            startActivity(intent);
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(ReserveInfoBean.DataBeanX.NewArrBean item, int position) {
                                    return 0;
                                }
                            };
                            rvType.setAdapter(valueBeanRBaseAdapter);
                            //合同
                            if (reserveInfoBean.getData().getData().getContracts() != null) {
                                mlist_hetong.clear();
                                mlist_rvhetong.clear();
                                for (int i = 0; i < reserveInfoBean.getData().getData().getContracts().size(); i++) {
                                    ReserveInfoBean.DataBeanX.DataBean.ContractsBean contractsBean = reserveInfoBean.getData().getData().getContracts().get(i);
                                    ContractsBean contractsBea = new ContractsBean();
                                    contractsBea.setContract_id(contractsBean.getContract_id());
                                    mlist_hetong.add(contractsBean.getContracts());
                                    contractsBea.setContracts(contractsBean.getContracts());
                                    mlist_rvhetong.add(contractsBea);
                                }

                                rvContract.setLayoutManager(new GridLayoutManager(ReservedInformationActivity.this, 3));

                                RBaseAdapter<String> rBaseAdapter = new RBaseAdapter<String>(
                                        ReservedInformationActivity.this, R.layout.item_img, mlist_hetong) {
                                    @Override
                                    protected void fillItem(RViewHolder holder, String item, int position) {
                                        ImageView img = holder.itemView.findViewById(R.id.img);
                                        Glide.with(ReservedInformationActivity.this).load(item).into(img);
                                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                ImageViewpagerDialog.show(ReservedInformationActivity.this, null, mlist_hetong, position);
                                            }
                                        });

                                    }

                                    @Override
                                    protected int getViewType(String item, int position) {
                                        return 0;
                                    }
                                };
                                rvContract.setAdapter(rBaseAdapter);
                            }
                            //其他
                            if (reserveInfoBean.getData().getData().getOthers() != null) {
                                mlist_rvqita.clear();
                                mlist_qita.clear();
                                for (int i = 0; i < reserveInfoBean.getData().getData().getOthers().size(); i++) {
                                    ReserveInfoBean.DataBeanX.DataBean.OthersBean othersBean1 = reserveInfoBean.getData().getData().getOthers().get(i);
                                    OthersBean othersBean = new OthersBean();
                                    othersBean.setOther_id(othersBean1.getOther_id());
                                    othersBean.setOthers(othersBean1.getOthers());
                                    mlist_rvqita.add(othersBean);
                                    mlist_qita.add(othersBean1.getOthers());
                                }
                                rvOther.setLayoutManager(new GridLayoutManager(ReservedInformationActivity.this, 3));
                                RBaseAdapter<String> rBaseAdapterother = new RBaseAdapter<String>(
                                        ReservedInformationActivity.this, R.layout.item_img, mlist_qita) {
                                    @Override
                                    protected void fillItem(RViewHolder holder, String item, int position) {
                                        ImageView img = holder.itemView.findViewById(R.id.img);
                                        Glide.with(ReservedInformationActivity.this).load(item).into(img);
                                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                ImageViewpagerDialog.show(ReservedInformationActivity.this, null, mlist_qita, position);
                                            }
                                        });

                                    }

                                    @Override
                                    protected int getViewType(String item, int position) {
                                        return 0;
                                    }
                                };
                                rvOther.setAdapter(rBaseAdapterother);
                            }
                            ReserveInfoBean.DataBeanX.RoleBean role = reserveInfoBean.getData().getRole();
                            List<ReserveInfoBean.DataBeanX.ScheduledDepositBean> scheduled_deposit = reserveInfoBean.getData().getScheduled_deposit();
                            if(role.getIs_say()==0&&role.getIs_modify()==1&&role.getIs_check()==1){
                                detele.setVisibility(View.VISIBLE);
                                textYd.setVisibility(View.GONE);
                            }else if(role.getIs_say()==0&&role.getIs_modify()==1&&role.getIs_check()==0){
                                detele.setVisibility(View.VISIBLE);
                                textYd.setVisibility(View.VISIBLE);
                                detele.setText("");
                                textYd.setVisibility(View.VISIBLE);
                            }
                        }

                    }
                });
    }


    public class OthersBean {
        /**
         * other_id : 91
         * others : http://lxy.yskxfs.com/uploads/uploads/apifile/20210423/09efaaf046905a94cb29fb342827eca7.png
         */

        private String other_id;
        private String others;

        public String getOther_id() {
            return other_id;
        }

        public void setOther_id(String other_id) {
            this.other_id = other_id;
        }

        public String getOthers() {
            return others;
        }

        public void setOthers(String others) {
            this.others = others;
        }
    }

    public class ContractsBean {
        /**
         * contract_id : 92
         * contracts : http://lxy.yskxfs.com/uploads/uploads/apifile/20210423/75d4d96f391a9497b768cbdd2683f42f.jpg
         */

        private String contract_id;
        private String contracts;

        public String getContract_id() {
            return contract_id;
        }

        public void setContract_id(String contract_id) {
            this.contract_id = contract_id;
        }

        public String getContracts() {
            return contracts;
        }

        public void setContracts(String contracts) {
            this.contracts = contracts;
        }
    }
}